#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMessageBox>
#include <QCoreApplication>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_ptrStudentSql(nullptr)
{
    ui->setupUi(this);
    m_ptrStudentSql = StudentSql::getinstance();

    updateTable();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::updateTable()
{
    QString s1="s_id like '"+ui->le_id->text()+"'";
    QString s2="s_name like '"+ui->le_name->text()+"'";
    QString s3="s_age>="+ui->le_ages->text()+" and s_age<="+ui->le_agee->text();
    QString s4="s_sex like '"+ui->le_sex->text()+"'";
    QString s5="s_class like '"+ui->le_class->text()+"'";
    QString s6="s_dept like '"+ui->le_dept->text()+"'";
    QString s7="s_addr like '"+ui->le_addr->text()+"'";
    bool b1=ui->cb_id->isChecked();
    bool b2=ui->cb_name->isChecked();
    bool b3=ui->cb_age->isChecked();
    bool b4=ui->cb_sex->isChecked();
    bool b5=ui->cb_class->isChecked();
    bool b6=ui->cb_dept->isChecked();
    bool b7=ui->cb_addr->isChecked();
    QString s="select * from student";
    bool b=false;
    if(b1)
    {
        if(b)
        {
            s=s+" and "+s1;
        }
        else
        {
            s=s+" where "+s1;
        }
        b=true;
    }
    if(b2)
    {
        if(b)
        {
            s=s+" and "+s2;
        }
        else
        {
            s=s+" where "+s2;
        }
        b=true;
    }
    if(b3)
    {
        if(b)
        {
            s=s+" and "+s3;
        }
        else
        {
            s=s+" where "+s3;
        }
        b=true;
    }
    if(b4)
    {
        if(b)
        {
            s=s+" and "+s4;
        }
        else
        {
            s=s+" where "+s4;
        }
        b=true;
    }
    if(b5)
    {
        if(b)
        {
            s=s+" and "+s5;
        }
        else
        {
            s=s+" where "+s5;
        }
        b=true;
    }
    if(b6)
    {
        if(b)
        {
            s=s+" and "+s6;
        }
        else
        {
            s=s+" where "+s6;
        }
        b=true;
    }
    if(b7)
    {
        if(b)
        {
            s=s+" and "+s7;
        }
        else
        {
            s=s+" where "+s7;
        }
        b=true;
    }
    s=s+";";
    ui->textEdit->setText(s);
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(7);
    QStringList l;
    l<<"学号"<<"姓名"<<"年龄"<<"性别"<<"班级"<<"系别"<<"地址";
    ui->tableWidget->setHorizontalHeaderLabels(l);
    QList<StudentInfo> lStuds = m_ptrStudentSql->getResult(s);
    ui->tableWidget->setRowCount(10);
    for(int i=0;i<lStuds.size();i++)
    {
        //qDebug()<<lStuds[i].s_name;
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(lStuds[i].s_id));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(lStuds[i].s_name));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(QString::number(lStuds[i].s_age)));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(lStuds[i].s_sex));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(lStuds[i].s_class));
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(lStuds[i].s_dept));
        ui->tableWidget->setItem(i,6,new QTableWidgetItem(lStuds[i].s_addr));
    }
}


void MainWindow::on_pushButton_clicked()
{
    updateTable();
}

